package me.zhanshi123.vipstorage.command;

import me.zhanshi123.vipstorage.MessageManager;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

public class CommandsExecutor implements CommandExecutor {
    public boolean onCommand(CommandSender commandSender, Command command, String label, String[] args) {
        if (args.length == 0) {
            sendHelp(commandSender);
            return false;
        } else {
            String arg = args[0];
            SubCommand subCommand = Commands.getInstance().getSubCommand(arg);
            if (subCommand == null) {
                subCommand = Commands.getInstance().getSubCommand("*");
            }
            if (subCommand == null) {
                return false;
            }
            if (subCommand.isOpCommand()) {
                if (commandSender.isOp()) {
                    return subCommand.onCommand(commandSender, command, label, args);
                } else {
                    commandSender.sendMessage(MessageManager.getString("permissionDeny"));
                    return true;
                }
            } else {
                return subCommand.onCommand(commandSender, command, label, args);
            }
        }
    }

    private void sendHelp(CommandSender sender) {
        MessageManager.getStringList("commandTips").forEach(msg -> sender.sendMessage(msg));
    }
}
